<template>
	<view class="back-view" @touchmove.stop.prevent="moveHandle">
		<view class="view-background"></view>
		<view class="white-view-back">
			<view class="top-back">
				<view class="top-name">关联客户</view>
				<view class="close-back" @click="closeBtnAction">
					<image class="close-img" src="../../../static/close.png"></image>
				</view>
			</view>

			<view class="top-line"></view>
			<view class="center-selectname">请选择你要关联的客户：</view>
			<scroll-view class="center-back" scroll-y="true">
				<view :class="selectIndex == index?'center-item center-item-select':'center-item' "
					@click="clickItemAction(index)" v-for="(item,index) in list" :key="index">{{item.name}}</view>
			</scroll-view>
			<view class="center-add">或直接 <text class="center-add-des" @click="addAssociateAction">新增客户</text></view>
			<view class="bottom-back">
				<view class="bottom-btn" @click="clickSureAction">确认购买</view>
			</view>
		</view>
		<addCustomer v-if="showAddCustomer" :editType='editType' v-on:successBack='updataRequest()'
			:rangeList='rangeList' :statusRangeList='statusRangeList' :hopeRangeList='hopeRangeList'
			@addCustomerClose='addCustomerClose'></addCustomer>
	</view>
</template>

<script>
	import addCustomer from "@/packageC/PhonePC/customer/addCustomer.vue"
	let app = ''

	export default {
		components: {
			addCustomer
		},
		data() {
			return {
				role: '', //当前登录角色
				roleUrl: '',
				list: [],
				showAddCustomer: false,
				editType: 'add',
				rangeList: [], //
				statusRangeList: [],
				hopeRangeList: [], //
				selectIndex: -1
			}
		},
		mounted() {
			var _this = this;
			app = getApp()
			_this.role = app.globalData.role
			_this.roleUrl = _this.$roleUrl({
				tab: _this.role
			})
			this.getList();
			this.requestDicData();
			this.requestStatusDicData();
			this.requestHopeDicData();
		},
		methods: {
			async updataRequest() {
				this.selectIndex = -1;
				this.getList()
			},
			async getList() {
				let _this = this
				let parmas = {
					page: 1,
					size: 10000
				}
				const res = await _this.$myRequest({
					url: `/${_this.roleUrl}/customer/list`,
					data: parmas,
					method: 'POST', //默认是GET
				})
				console.log('关联', res)
				if (res.data.code == 200) {
					_this.list = res.data.data.list
					// for (var i = 0; i < list.length; i++) {
					// 	_this.dataList.push(list[i])
					// }

				}
			},

			//确认购买
			clickSureAction() {
				if (this.selectIndex == -1) {
					uni.showToast({
						title: '请选择你要关联的客户',
						icon: 'none'
					})
					return;
				}else{
				var item = this.list[this.selectIndex];	
				console.log(item)
				this.$emit('selectCust',item)
				
				}
			},

			//客户类型
			async requestDicData() {
				let _this = this
				const res = await this.$myRequest({
					url: "/comm/dict/list/param_customer_type",
					method: 'GET',
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					var arr = [];
					for (var i = 0; i < res.data.data.length; i++) {
						var item = res.data.data[i]
						item.label = item.name;
						arr.push(item)
					}
					_this.rangeList = arr;
					console.log(_this.rangeList)
				}
			},
			//权限类型
			async requestStatusDicData() {
				let _this = this
				const res = await this.$myRequest({
					url: "/comm/dict/list/param_customer_perm",
					method: 'GET',
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					var arr = [];
					for (var i = 0; i < res.data.data.length; i++) {
						var item = res.data.data[i]
						item.label = item.name;
						arr.push(item)
					}
					_this.statusRangeList = arr;
					console.log(_this.statusRangeList)
				}
			},
			//组别标签
			async requestHopeDicData() {
				let _this = this
				const res = await this.$myRequest({
					url: "/comm/dict/list/param_customer_group",
					method: 'GET',
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					var arr = [];
					for (var i = 0; i < res.data.data.length; i++) {
						var item = res.data.data[i]
						item.label = item.name;
						arr.push(item)
					}
					_this.hopeRangeList = arr;
					console.log(_this.hopeRangeList)
				}
			},
			clickItemAction(index) {
				if (this.selectIndex == index) {
					this.selectIndex = -1;
				} else {
					this.selectIndex = index;
				}
				// 	this.list[index].select = !this.list[index].select
			},
			closeBtnAction() {
				this.$emit('associtateCustomerClose')
			},
			
			moveHandle() {
				return true
			},
			//点击新增客户
			addAssociateAction() {
				this.editType = 'add';
				this.showAddCustomer = true
			},
			//隐藏新增客户
			addCustomerClose() {
				this.showAddCustomer = false
			}
		},
	}
</script>

<style>
	.back-view {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 1099;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.view-background {
		position: absolute;
		width: 100%;
		height: 100%;
		z-index: 100;
		background-color: rgba(0, 0, 0, 0.3);
	}

	.white-view-back {
		width: 348px;
		height: 437px;
		padding: 24px;
		max-height: 540px;
		background-color: white;
		margin-top: -70px;
		border-radius: 4px;
		z-index: 101;
		position: absolute;
	}

	.top-back {
		display: flex;
		justify-content: space-between;
	}

	.top-name {
		font-size: 18px;
		font-weight: normal;
		color: #333333;
	}

	.close-back {
		width: 20px;
		height: 20px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.close-img {
		width: 12px;
		height: 12px;
	}

	.top-line {
		height: 1px;
		background: #EEEEEE;
		width: 100%;
		margin-top: 20px;
		margin-bottom: 20px;
	}

	.bottom-back {
		bottom: 0px;
		width: 100%;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		position: absolute;
		height: 60px;
		right: 16px;
	}

	.bottom-btn {
		cursor: pointer;
		width: 104px;
		height: 32px;
		background: #2DA9C5;
		border-radius: 2px;
		color: #FFFFFF;
		font-size: 14px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.center-back {
		max-height: 280px;
	}

	.center-selectname {
		font-size: 16px;
		font-weight: 600;
		color: #333333;
		margin-top: 24px;
	}

	.center-add {
		font-size: 14px;
		font-family: PingFangSC-Regular, PingFang SC;
		color: #333333;
		margin-top: 10px;
	}

	.center-add-des {
		color: #2DA9C5;
		margin-left: 10px;
	}

	.center-item {
		width: 300px;
		padding: 16px;
		font-size: 16px;
		font-weight: 500;
		color: #666666;
		background: #EEEEEE;
		border-radius: 4px;
		margin-top: 10px;
	}

	.center-item-select {
		background: #E8F7FB;
		color: #2DA9C5;
	}
</style>
